﻿using System;
using System.Collections;
using System.Data;
using System.Data.Common;
using System.Data.Odbc;

namespace app.scrum
{
    public class ScrumObjectHelper
    {

        public static ScrumObject getScrumObject(ArrayList sObjList, string key, object value)
        {
            foreach (ScrumObject sObj in sObjList)
            {
                ScrumField sField = (ScrumField)sObj.fieldMap[key];
                if (sField.value.Equals(value))
                {
                    return sObj;
                }
            }
            return null;
        }

        public static ScrumField getScrumField(ScrumObject sObj, string key)
        {
            return (ScrumField)sObj.fieldMap[key];
        }

        public static object getScrumFieldValue(ScrumObject sObj, string key)
        {
            ScrumField sField = (ScrumField)sObj.fieldMap[key];
            if (sField == null)
            {
                return null;
            }
            return sField.value;
        }

        public static void assignCandidateValues(ArrayList sObjList, string name, string candidateValues)
        {
            foreach (ScrumObject sObj in sObjList)
            {
                ScrumField sField = (ScrumField)sObj.fieldMap[name];
                sField.candidateValues = candidateValues;
                sField.typeName = ScrumField.typeOptions_Lookup;
            }
        }

        public static void assignDisplay(ArrayList sObjList, string name, ArrayList sObjList2)
        {
            foreach (ScrumObject sObj in sObjList)
            {
                ScrumField sField = (ScrumField)sObj.fieldMap[name];
                ScrumObject sObj2 = getScrumObject(sObjList2, ScrumField.fieldName_Id, sField.value);
                if (sObj2 != null)
                {
                    ScrumField sField2 = (ScrumField)sObj2.fieldMap[ScrumField.fieldName_Name];
                    sField.display = sField2.value == null ? "" : sField2.value.ToString();
                }
                sField.typeName = ScrumField.typeOptions_Lookup;
            }
        }

        public static void assignLookup(ScrumObject sObj, ArrayList sObjList, string lookupName)
        {
            ArrayList sObjSimpleList = sObj.getScrumObjectSimpleList(lookupName);
            string sObjSimpleListJson = JsonHelper.getJson(sObjSimpleList);
            string sObjSimpleListJsonJsonRecPart = JsonHelper.getJsonRecordPartFromJson(sObjSimpleListJson);
            string candidateValues = JsonHelper.jsonRecPart2Array(sObjSimpleListJsonJsonRecPart);
            //lookupName = Util.toUpperFirstChar(lookupName);
            ScrumObjectHelper.assignCandidateValues(sObjList, lookupName, candidateValues);
            ScrumObjectHelper.assignDisplay(sObjList, lookupName, sObjSimpleList);
        }
    }
}